﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.ServiceModel;
using System.Xml;

namespace LSWProxy
{
    public class LSWStoreProxy
    {
        string result;
        private ProxyService.StoreServiceClient  _proxy;
        private AutoResetEvent _autoResetEventStoreService;

        public LSWStoreProxy(string wcfServiceAddress)  
        {
            var endPointAddress = new EndpointAddress(wcfServiceAddress);
            var binding = new BasicHttpBinding();

            _autoResetEventStoreService = new AutoResetEvent(false);

            _proxy = new ProxyService.StoreServiceClient(binding, endPointAddress);

            _proxy.ExcuteStoreCompleted += new EventHandler<ProxyService.ExcuteStoreCompletedEventArgs>(_proxy_ExcuteStoreCompleted);
            _proxy.ExcuteSQLCompleted += new EventHandler<ProxyService.ExcuteSQLCompletedEventArgs>(_proxy_ExcuteSQLCompleted);
        }

        void _proxy_ExcuteSQLCompleted(object sender, ProxyService.ExcuteSQLCompletedEventArgs e)
        {
            result = e.Result;
            _autoResetEventStoreService.Set();
        }


        void _proxy_ExcuteStoreCompleted(object sender, ProxyService.ExcuteStoreCompletedEventArgs e)
        {
            result = e.Result;
            _autoResetEventStoreService.Set();
        }

        public string ExcuteStore(string NameStore,System.Collections.ObjectModel.ObservableCollection<object> Paramaters)
        {
            result = "";
            _proxy.ExcuteStoreAsync(NameStore, Paramaters);
            _autoResetEventStoreService.WaitOne();
            return result;
        }

        public string ExcuteSQL(string SQL)
        {
            result = "";
            _proxy.ExcuteSQLAsync(SQL);
            _autoResetEventStoreService.WaitOne();
            return result;
        }
    }
}
